<template>
<div id="home">
    <nav-bar class="home-nav"><div slot="center">超级商场</div></nav-bar>

    <h1>首页</h1>

</div>

</template>

<script>
import NavBar from "@/components/common/navbar/NavBar";


import {getHomeMultidata, getHomeData, BANNER, RECOMMEND} from "@/api/home";
import {POP, NEW, SELL, BACK_TOP_DISTANCE} from "@/common/const";
import {backTopMixin, itemListenerMixin} from "@/common/mixins";
import {debounce} from "@/common/utils";
export default {
  name: "Home",
  data(){
    return{
      banners: [],
      recommends: [],
      goodsList: {
        'pop': {page: 1, list: []},
        'new': {page: 1, list: []},
        'sell': {page: 1, list: []},
      },
      currentType: POP,
      tabOffControl: 0,
      isTabFixed: false,
      saveY: 0,
    }
  },
  components:{
    NavBar
  },
  methods:{
    /**
     *事件监听相关方法
     */
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = POP
          break
        case 1:
          this.currentType = NEW
          break
        case 2:
          this.currentType = SELL
          break
      }
      this.$refs.tabControl1.currentIndex = index
      this.$refs.tabControl2.currentIndex = index
    },
    contentScroll(position) {
      //判断backTop是否显示
      this.isShowBackTop = (-position.y) > BACK_TOP_DISTANCE
      //  决定tabControl是否吸顶（position: fixed）
      this.isTabFixed = (-position.y) > this.tabOffControl
    },
    loadMore() {
      this.getHomeProducts(this.currentType)
    },
    swiperImageLoad() {
      //  所有的组件都有一个属性$el：用于获取组件中的元素
      this.tabOffControl = this.$refs.tabControl2.$el.offsetTop
    },

    /**
     * 网络请求相关方法
     */
    getMultiData() {
      getHomeMultidata().then(res => {
        this.banners = res.data[BANNER].list
        this.recommends = res.data[RECOMMEND].list
        // this.$refs.scroll.scroll.refresh()
        // 下次更新DOM时,获取新的tabOffsetTop值(不保险,可以在updated钩子中获取)
        // this.$nextTick(() => {
        //   this.tabOffsetTop = this.$refs.tabControl.$el.offsetTop
        // })
      })
    },
    //商品数据请求
    getHomeProducts(type) {
      getHomeData(type, this.goodsList[type].page).then(res => {
        const goodsList = res.data.list;
        // push(...数组名)是将数组中的元素依次解析并push到list里去。
        this.goodsList[type].list.push(...goodsList)
        this.goodsList[type].page += 1
        // 完成下拉加载更多 再次请求数据调用
        this.$refs.scroll.finishPullUp()
      })
    }
  },
  created() {
    //请求多个数据
    this.getMultiData()
    //  请求商品数据
    this.getHomeProducts(POP)
    this.getHomeProducts(NEW)
    this.getHomeProducts(SELL)
  },
  computed:{
    showGoods() {
      return this.goodsList[this.currentType].list
    }
  },
  mixins: [itemListenerMixin, backTopMixin],
  activated() {
    // this.$refs.scroll.scrollTo(0, this.saveY, 0)
    // this.$refs.scroll.refresh()
    // this.$bus.$on('itemImagesLoad', this.itemListener)
  },
  deactivated() {
    // this.saveY = this.$refs.scroll.getScrollY()
    // this.$bus.$off('itemImagesLoad', this.itemListener)
  },
  updated() {
    //zhihaot1：当back-top组件出现时，调用updated，刷新scrollerHeight
    //当有数据接口时，还是使用mounted里面的方法
    // this.$refs.scroll.refresh()
    // console.log('updated');
  }
}
</script>

<style scoped>
#home {
  /* 这里也可以加相对定位，给下面的scroll（.content）做锚点 */
  /*position: relative;*/
  /*padding-top: 44px;*/
  /* 100%视口的高度  两种方案都需要父盒子的高度 */
  height: 100vh;
}

.home-nav {
  background-color: var(--color-tint);
  color: #fff;
}
</style>
